﻿namespace Ross.BSCSystem.Controllers
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    using System.Web;
    using System.Web.Http;
    using System.Web.Http.Results;
    using Ross.BSCSystem;
    using Ross.BSCSystem.Controllers;
    using Ross.EntityFramework;
    using Ross.EntityFramework.Model;
    using Ross.Service;
    using Ross.Service.Dto;

    [SimpleAuthenticationFilter]
    public class TargetController : BaseApiController
    {
        //总表
        [HttpPost]
        public JsonResult<ResultDto<bool>> Insert([FromBody] Service.Dto.NewZongBiao target)
        {
            ResultDto<bool> result = new ResultDto<bool>();
            using (TargetService serv = new TargetService(dbContextStr))
            {
                result = serv.NewInsert(target);
            }

            return Json(result);
        }

        [HttpPost]
        public JsonResult<ResultDto<bool>> InsertC([FromBody] Service.Dto.TargetsOutput target)
        {
            ResultDto<bool> result = new ResultDto<bool>();
            using (TargetService serv = new TargetService(dbContextStr))
            {
                result = serv.InsertZi(target);
            }

            return Json(result);
        }

        //部门输入
        [HttpPost]
        public JsonResult<ResultDto<bool>> Insertbumen([FromBody] Service.Dto.NewZongBiao target)
        {
            ResultDto<bool> result = new ResultDto<bool>();
            using (TargetService serv = new TargetService(dbContextStr))
            {
                result = serv.NewInsert(target);
            }

            return Json(result);
        }

        public JsonResult<ResultDto<List<Service.Dto.TargetOutput>>> GetTreeList(string keywords = "", string departId = "", string status = "", string timespan = "")
        {
            var result = new ResultDto<List<Service.Dto.TargetOutput>>();
            using (BuMenService serv = new BuMenService(dbContextStr))
            {
                result = serv.GetTreeList(keywords, departId, status, timespan);
            }
            return Json(result);
        }

        //总表、部门级更新
        [HttpPost]
        public JsonResult<ResultDto<bool>> Update([FromBody] Service.Dto.NewZongBiao target)
        {
            ResultDto<bool> result = new ResultDto<bool>();
            using (TargetService serv = new TargetService(dbContextStr))
            {
                result = serv.NewUpdate(target);
            }

            return Json(result);
        }

        //部门
        [HttpPost]
        public JsonResult<ResultDto<bool>> Updatebumen([FromBody] Service.Dto.NewZongBiao target)
        {
            ResultDto<bool> result = new ResultDto<bool>();
            using (TargetService serv = new TargetService(dbContextStr))
            {
                result = serv.NewUpdate(target);
            }

            return Json(result);
        }

        [HttpPost]
        public JsonResult<ResultDto<bool>> Delete(int id)
        {
            ResultDto<bool> result = new ResultDto<bool>();
            using (TargetService serv = new TargetService(dbContextStr))
            {
                result = serv.NewDelete(id);
            }

            return Json(result);
        }

        [HttpPost]
        public JsonResult<ResultDto<bool>> Deletebumen(int id)
        {
            ResultDto<bool> result = new ResultDto<bool>();
            using (TargetService serv = new TargetService(dbContextStr))
            {
                result = serv.NewDelete(id);
            }

            return Json(result);
        }

        //审核
        [HttpPost]
        public JsonResult<ResultDto<bool>> Approve(int ID, string ApprovalStatus)
        {
            ResultDto<bool> result = new ResultDto<bool>();
            using (TargetService serv = new TargetService(dbContextStr))
            {
                result = serv.ApproveTarget(ID, ApprovalStatus);
            }

            return Json(result);
        }

        //发布
        [HttpPost]
        public JsonResult<ResultDto<bool>> FaBu(int ID, bool ApprovalStatus)
        {
            ResultDto<bool> result = new ResultDto<bool>();
            using (TargetService serv = new TargetService(dbContextStr))
            {
                result = serv.FaBu(ID, ApprovalStatus);
            }

            return Json(result);
        }

        [HttpGet]
        public JsonResult<ResultDto<List<Service.Dto.TargetOutput>>> Query()
        {
            var result = new ResultDto<List<Service.Dto.TargetOutput>>();
            using (TargetService serv = new TargetService(dbContextStr))
            {
                result = serv.NewChaXunBuMen();
            }

            return Json(result);
        }

        [HttpPost]
        public JsonResult<ResultDto<Targets>> GetById(int id)
        {
            var result = new ResultDto<Targets>();
            using (TargetService serv = new TargetService(dbContextStr))
            {
                result = serv.GetTarget(id);
            }

            return Json(result);
        }

        [HttpGet]
        public JsonResult<ResultDto<string>> GetDepartment()
        {
            var result = new ResultDto<string>();
            using (TargetService serv = new TargetService(dbContextStr))
            {
                result = serv.GetDepartment();
            }
            return Json(result);
        }

        [HttpGet]
        public JsonResult<ResultDto<string>> GetShouQuanBuMen()
        {
            var result = new ResultDto<string>();
            using (TargetService serv = new TargetService(dbContextStr))
            {
                result = serv.GetShouQuan();
            }
            return Json(result);
        }


        [HttpGet]
        public JsonResult<ResultDto<List<Targets>>> GetResourceTarget()
        {
            var result = new ResultDto<List<Targets>>();
            using (TargetService serv = new TargetService(dbContextStr))
            {
                result = serv.GetResourceTarget();
            }

            return Json(result);
        }

        [HttpPost]
        public JsonResult<ResultDto<bool>> GaiTargetState(int ID, string Status)
        {
            ResultDto<bool> result = new ResultDto<bool>();
            using (TargetService serv = new TargetService(dbContextStr))
            {
                result = serv.GaiTargetState(ID, Status);
            }

            return Json(result);
        }

        
        [HttpPost]
        public JsonResult<ResultDto<bool>> XiuBian([FromBody] Service.Dto.BuMenBiaoOutput target)
        {
            var result = new ResultDto<bool>();
            using (TargetService serv = new TargetService(dbContextStr))
            {
                result = serv.XiuBian(target);
            }
            return Json(result);
        }

        //版本显示
        [HttpGet]
        public JsonResult<ResultDto<List<History>>> GetVersion(int id)
        {
            var result = new ResultDto<List<History>>();
            using (TargetService serv = new TargetService(dbContextStr))
            {
                result = serv.ShowVersion(id);
            }
            return Json(result);
        }


        
        [HttpPost]
        public JsonResult<ResultDto<bool>> InsertZhanLue([FromBody] Service.Dto.ZhanLueInput input)
        {
            ResultDto<bool> result = new ResultDto<bool>();
            using (TargetService serv = new TargetService(dbContextStr))
            {
                result = serv.ZhanLueInsert(input);
            }

            return Json(result);
        }

        [HttpPost]
        public JsonResult<ResultDto<bool>> InsertBuMen2([FromBody]Service.Dto.BuMenInput input)
        {
            ResultDto<bool> result = new ResultDto<bool>();
            using (TargetService serv = new TargetService(dbContextStr))
            {
                result = serv.BuMenInsert(input);
            }

            return Json(result);
        }

        [HttpPost]
        public JsonResult<ResultDto<bool>> UpdateZhanLue([FromBody] Service.Dto.ZhanLueInput input)
        {
            ResultDto<bool> result = new ResultDto<bool>();
            using (TargetService serv = new TargetService(dbContextStr))
            {
                result = serv.ZhanLueUpdate(input);
            }

            return Json(result);
        }

        [HttpPost]
        public JsonResult<ResultDto<bool>> UpdateBuMen2([FromBody]Service.Dto.BuMenInput input)
        {
            ResultDto<bool> result = new ResultDto<bool>();
            using (TargetService serv = new TargetService(dbContextStr))
            {
                result = serv.BuMenUpdate(input);
            }

            return Json(result);
        }

        [HttpGet]
        public JsonResult<ResultDto<List<ShenHeOutput>>> Test()
        {
            var result = new ResultDto<List<ShenHeOutput>>();
            using (TargetService serv = new TargetService(dbContextStr))
            {
                result = serv.ChaXunShenHe();
            }

            return Json(result);
        }

        [HttpPost]
        public JsonResult<ResultDto<bool>> XiubianZhanlue([FromBody]Service.Dto.ZhanLueInput input)
        {
            ResultDto<bool> result = new ResultDto<bool>();
            using (TargetService serv = new TargetService(dbContextStr))
            {
                result = serv.XiuBianZhanlue(input);
            }

            return Json(result);
        }
        [HttpPost]
        public JsonResult<ResultDto<bool>> XiubianBumen([FromBody]Service.Dto.BuMenInput input)
        {
            ResultDto<bool> result = new ResultDto<bool>();
            using (TargetService serv = new TargetService(dbContextStr))
            {
                result = serv.XiuBianBumen(input);
            }

            return Json(result);
        }

        //部门
        [HttpPost]
        public JsonResult<ResultDto<bool>> PiLiangInsert([FromBody] Service.Dto.BuMenInput2 input)
        {
            ResultDto<bool> result = new ResultDto<bool>();
            using (TargetService serv = new TargetService(dbContextStr))
            {
                result = serv.BuMenPiLiangInsert(input);
            }

            return Json(result);
        }
    }
}